/*
 * MainScreen.java
 *
 * Created on Mar 30, 2011, 4:22:38 PM
 */

package forumGUI;

import client.ForumClientListener;
import client.TcpClient;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import messaage.*;

/**
 * @author Batel
 */
public class MainScreen extends javax.swing.JFrame implements
		client.MessagesListener {

	private TcpClient client;

	/** Creates new form MainScreen */
	public MainScreen(TcpClient _client) {
		this.client = _client;
		initComponents();
                 this.setBackground();
	}

	public TcpClient getTcpClient() {
		return this.client;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LoginLabel = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        LoginButton = new javax.swing.JButton();
        newUserLabel = new javax.swing.JLabel();
        registerButton = new javax.swing.JButton();
        PasswordField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        LoginLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 48));
        LoginLabel.setText("Welcome!");

        usernameTextField.setText("k");

        usernameLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        usernameLabel.setText("Username:");

        passwordLabel.setFont(new java.awt.Font("Viner Hand ITC", 0, 24));
        passwordLabel.setText("Password:");

        LoginButton.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        LoginButton.setText("Login");
        LoginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LoginButtonActionPerformed(evt);
            }
        });

        newUserLabel.setFont(new java.awt.Font("Viner Hand ITC", 1, 24));
        newUserLabel.setText("New user?");

        registerButton.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18));
        registerButton.setText("Register now!");
        registerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordLabel)
                            .addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                            .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(169, 169, 169)
                        .addComponent(newUserLabel)
                        .addGap(18, 18, 18)
                        .addComponent(registerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(73, 73, 73))
            .addGroup(layout.createSequentialGroup()
                .addGap(202, 202, 202)
                .addComponent(LoginLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(160, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(51, Short.MAX_VALUE)
                .addComponent(LoginLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(passwordLabel)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(LoginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newUserLabel)
                    .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(102, 102, 102))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

        private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
                this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
                DisconnectMsg msg = new DisconnectMsg(null);  
                //TODO change the username to send
                this.client.sendMessage(msg);
        }//GEN-LAST:event_formWindowClosed

	private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_LoginButtonActionPerformed
		String username = this.usernameTextField.getText();
		String password = this.PasswordField.getText();
		if (!username.equals("") && !password.equals("")) {
			LoginMsg msg = new LoginMsg(username, password);
			this.client.sendMessage(msg);
		} else
			JOptionPane.showMessageDialog(this,
					"Wrong username or password. Please try again.");

	}// GEN-LAST:event_LoginButtonActionPerformed

	private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_registerButtonActionPerformed
		RegisterScreen regScreen = new RegisterScreen(this.client, this);
		
                 ForumClientListener tempListener = (ForumClientListener) this.client
				.getListener();
		tempListener.getConnector().addListener(regScreen);
		this.setVisible(false);
		regScreen.setVisible(true);
	}// GEN-LAST:event_registerButtonActionPerformed


         public void setBackground()
    {
        Container con =this.getContentPane();
        JPanel panelBgImg;
        con.setLayout(null);
        final String photoPath = "src/images/screen.jpg";
        ImageIcon imh = new ImageIcon(photoPath);
//        setSize(imh.getIconWidth(), imh.getIconHeight());

        panelBgImg = new JPanel()
        {
            public void paintComponent(Graphics g)
            {
                Image img = new ImageIcon(photoPath).getImage();

                Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
                setPreferredSize(size);
                setMinimumSize(size);
                setMaximumSize(size);
                setSize(size);
                setLayout(null);
                g.drawImage(img, 0, 0, null);
            }
        };

        con.add(panelBgImg);
        panelBgImg.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());

    }

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				// TcpClient client = new TcpClient();
				// new MainScreen(client).setVisible(true);
			}
		});
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton LoginButton;
    private javax.swing.JLabel LoginLabel;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel newUserLabel;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JButton registerButton;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration//GEN-END:variables

	@Override
	public void loggedIn(LoggedInMsg msg) {
		String usrename = this.usernameTextField.getText();
		forumsScreen fs = new forumsScreen(this.client, usrename, msg.isModerator());
		ForumClientListener tempListener = (ForumClientListener) this.client
				.getListener();
		tempListener.getConnector().addListener(fs);
		GetListOfForumsMsg listMsg = new GetListOfForumsMsg(usrename);
		this.client.sendMessage(listMsg);
		this.setVisible(false);

	}

	@Override
	public void disconnected(DisconnectedMsg msg) {
                this.dispose();
	}

	@Override
	public void registered(RegisteredMsg msg) {

	}

	@Override
	public void errorMessage(ErrorMsg msg) {
		if (this.isVisible() == true){
			String body = msg.getBody();
			JOptionPane.showMessageDialog(this, body);
		}
	}

	@Override
	public void connected(ConnectedMsg msg) {

	}

	@Override
	public void loggedOff(LoggedOffMsg msg) {

	}

	@Override
	public void gotListOfForums(GotListOfForumsMsg msg) {

	}

	@Override
	public void gotTopicsOfForum(GotTopicsOfForumMsg msg) {

	}

	@Override
	public void gotThread(GotThreadMsg msg) {

	}

	@Override
	public void addedPost(AddedPostMsg msg) {

	}

	@Override
	public void createdThread(CreatedThreadMsg msg) {

	}

	@Override
	public void addedForum(AddedForumMsg msg) {

	}

	@Override
	public void removedForum(RemovedForumMsg msg) {

	}

	@Override
	public void editedPost(EditedPostMsg msg) {

	}

	@Override
	public void removedUser(RemovedUserMsg msg) {

	}

	@Override
	public void removedThread(RemovedThreadMsg msg) {

	}

	@Override
	public void removedPost(RemovedPostMsg msg) {

	}

	@Override
	public void addedFriend(AddedFriendMsg msg) {

	}

	@Override
	public void removedFriend(RemovedFriendMsg msg) {

	}

	@Override
	public void gotFriendsList(GotFriendsListMsg msg) {
	}

}
